﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoUser;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class UserRepository : BaseRepository<UserInfo>, IUserRepository
    {
        public UserRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<List<UserInfoDto>> GetAllUserInfoAsync(QueryUserDto where)
        {
            var exp = Expressionable.Create<UserInfo, RoleInfo>();
            exp.AndIF(!string.IsNullOrEmpty(where.UserName), (it, i) => it.UserName == where.UserName);
            exp.AndIF(!string.IsNullOrEmpty(where.FullName), (it, i) => it.FullName == where.FullName);
            exp.AndIF(!string.IsNullOrEmpty(where.Department), (it, i) => it.Department == where.Department);
            exp.AndIF(!string.IsNullOrEmpty(where.JobNumber), (it, i) => it.JobNumber == where.JobNumber);
            exp.AndIF(true, (it, i) => it.IsDelete == false);

            var result = await this.Context.Queryable<UserInfo, RoleInfo>((it, i) => new JoinQueryInfos(JoinType.Left, it.RoleID == i.ID))
                    .Where(exp.ToExpression())
                    .Select((it, i) => new UserInfoDto { UserID = it.UserID.SelectAll(), RoleFullName = i.FullName })
                   .ToListAsync();
            return result;
        }

        public async Task<bool> InitPasswordAsync(List<string> userNames)
        {
            string[] alluserName = userNames.ToArray();
            var result = await this.Context.Updateable<UserInfo>()
                 .SetColumns(it => it.Password == GenerateMD5.CreateMD5("8888"))
                  .Where(it => alluserName.Contains(it.UserName))
                 .ExecuteCommandAsync();

            return result > 0;
        }

        public async Task<bool> LoginCheckUserNameAsync(string userName)
        {
            return await this.Context.Queryable<UserInfo>()
                .Where(it => it.UserName == userName).AnyAsync();
        }

        public async Task<UserInfo> LoginAsync(string userName, string password)
        {
            var result = await this.Context.Queryable<UserInfo>()
             .FirstAsync(it => it.UserName == userName && it.Password == password);
            return result;
        }

        public async Task<bool> UpdateUserInfoAsync(UserInfo userInfo)
        {
            var result = await this.Context.Updateable<UserInfo>()
                .SetColumnsIF(true, it => it.IcCode == userInfo.IcCode)
                .SetColumnsIF(!string.IsNullOrEmpty(userInfo.Department), it => it.Department == userInfo.Department)
                .SetColumnsIF(!string.IsNullOrEmpty(userInfo.JobNumber), it => it.JobNumber == userInfo.JobNumber)
                .SetColumnsIF(userInfo.RoleID > 0, it => it.RoleID == userInfo.RoleID)
                .SetColumnsIF(!string.IsNullOrEmpty(userInfo.FullName), it => it.FullName == userInfo.FullName)
                .SetColumnsIF(true, it => it.UpdateTime == DateTime.Now)
                .Where(it => it.UserID == userInfo.UserID)
                .ExecuteCommandAsync();

            if (result > 0) return true;
            else
                return false;
        }

        public async Task<bool> UpdatePasswordAsync(long id, string newPassword)
        {
            newPassword = GenerateMD5.CreateMD5(newPassword);
            var result = await this.Context.Updateable<UserInfo>()
                  .SetColumns(it => it.Password == newPassword)
                  .Where(it => it.UserID == id)
                  .ExecuteCommandAsync();
            if (result > 0) return true;
            else
                return false;
        }

        public async Task<bool> DeleteUserInfoAsync(long userID)
        {
            var result = await this.Context.Updateable<UserInfo>()
                    .SetColumns(it => it.IsDelete == true)
                    .Where(it => it.UserID == userID)
                    .ExecuteCommandAsync();
            if (result > 0) return true;
            else
                return false;
        }

    }
}
